unit mForms;
{$i mPlatform.inc}
interface

uses
  Classes, SysUtils, mControls, mSets, mGraphics;
{ TForm }
 type
 TForm=class(TWinControl)
 private
   FOnClose: TNotifyEvent;
 protected
   FFormExtData:AnsiString;//Structure MUSTN'T be platform dependent
   function GetRealWindow: boolean; override;
   procedure DoCreate(var AMSG:mSets.TMessage);message WM_Create;
   procedure DoPaint(var AMSG:mSets.TMessage);message wm_paint;
   procedure DoResize(var AMSG:mSets.TMessage);message wm_size;
   procedure SetCaption(AValue: AnsiString); override;
 public
   constructor Create(AOwner: TComponent); override;
   destructor Destroy; override;
   property Canvas;
 published
   property OnClick;
   property OnResize;
   property Color;
   property OnPaint;
   property OnClose:TNotifyEvent read FOnClose write FOnClose;
   property Caption;
//   property
 end;

 { TApplication }

 TApplication = class(TComponent)
 protected
   FTerminating:boolean;
 public
   procedure ProcessMessages;
   procedure Run;
   procedure Init;
   procedure Terminate;
   procedure Done;
 end;
var Application:TApplication;
implementation

{$ifdef win}
{$i mformswin.inc}
{$endif}

{$ifdef unix}
{$i mformsunx.inc}
{$endif}

{ TApplication }


procedure TApplication.Run;
begin
  While not FTerminating do
  begin
   ProcessMessages;
  end;
end;

procedure TApplication.Init;
begin
  //TODO
end;

procedure TApplication.Terminate;
begin
  FTerminating:=true;
end;

procedure TApplication.Done;
var
  i: Integer;
begin
  for i:=0 to AllWnds.Count-1 do
    if AllWnds.Items[i]<>nil then
     if TObject(AllWnds.Items[i]) is TWinControl then
      TWinControl(AllWnds.Items[i]).Free;

end;


{ TForm }

function TForm.GetRealWindow: boolean;
begin
  Result:=true;
end;

procedure TForm.DoCreate(var AMSG: mSets.TMessage);
begin
  AMSG.Result:=0;
end;

procedure TForm.DoPaint(var AMSG: MSets.TMessage);
const   NoSelfRepaint:Boolean=false;
var
  OldColor: TColor;
  i: Integer;
  OldBackColor: TColor;

begin
//  Writeln('Painting ',Width,' ',Height,' ',Canvas.Handle);
  if FHandle=0 then exit;
  if Canvas.Handle=0 then Canvas.ActualizeWin(FHandle);
  OldColor:=Canvas.Color;
  OldBackColor:=Canvas.BackgroundColor;
  Canvas.Color:=Color;
  Canvas.BackgroundColor:=Color;
  if NoSelfRepaint then exit;
  NoSelfRepaint:=true;
  {$ifndef win} //Why custom controls draw itself behind parent?
  writeln('Drawing form with ',Canvas.Color,' and ',Canvas.BackgroundColor,' on ',Canvas.Handle);
  Canvas.FilledRectangle(0,0,Width-2,Height-1);
  {$endif}
  Canvas.MoveTo(0,0);
  AMsg.Result:=0;
  Canvas.Color:=OldColor;
  Canvas.BackgroundColor:=OldBackColor;
  for i:=0 to ControlCount-1 do
    Controls[i].Repaint;
  NoSelfRepaint:=false;
end;

type TLoHiLong=record
  Lo,Hi:SmallInt;
end;
procedure TForm.DoResize(var AMSG: mSets.TMessage);
var FLooped:Boolean;
begin
  FWidth:=AMSG.LParamLo;
  FHeight:=AMSG.LParamHi;

  if Assigned(OnResize) then OnResize(self);
  AMSG.Result:=0;
end;


initialization
  Application:=TApplication.Create(nil);
  PlatformInit_mForms;
finalization
  Application.Free
end.
